using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 租户资源配额实体
    /// 用于管理每个租户的资源使用限制和当前使用情况
    /// </summary>
    [Table("tenant_resource_quotas")]
    public class TenantResourceQuota : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 资源类型
        /// 如：storage, api_calls, users, databases等
        /// </summary>
        [Required]
        [StringLength(100)]
        public string ResourceType { get; set; } = string.Empty;

        /// <summary>
        /// 资源限制值
        /// 该租户对此资源的最大使用量
        /// </summary>
        public long LimitValue { get; set; }

        /// <summary>
        /// 当前使用值
        /// 该租户当前对此资源的使用量
        /// </summary>
        public long CurrentUsage { get; set; }

        /// <summary>
        /// 计量单位
        /// 如：bytes, count, requests等
        /// </summary>
        [StringLength(50)]
        public string? Unit { get; set; }

        /// <summary>
        /// 重置周期
        /// 使用量重置的时间周期（分钟）
        /// </summary>
        public int? ResetPeriodMinutes { get; set; }

        /// <summary>
        /// 上次重置时间
        /// </summary>
        public DateTime? LastResetAt { get; set; }

        /// <summary>
        /// 软限制阈值百分比
        /// 达到此百分比时发出警告（0-100）
        /// </summary>
        public int SoftLimitPercentage { get; set; } = 80;

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 备注说明
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }

        // 计算属性

        /// <summary>
        /// 使用率百分比
        /// </summary>
        [NotMapped]
        public decimal UsagePercentage => LimitValue > 0 ? Math.Round((decimal)CurrentUsage / LimitValue * 100, 2) : 0;

        /// <summary>
        /// 剩余配额
        /// </summary>
        [NotMapped]
        public long RemainingQuota => Math.Max(0, LimitValue - CurrentUsage);

        /// <summary>
        /// 是否超出软限制
        /// </summary>
        [NotMapped]
        public bool IsOverSoftLimit => UsagePercentage >= SoftLimitPercentage;

        /// <summary>
        /// 是否超出硬限制
        /// </summary>
        [NotMapped]
        public bool IsOverHardLimit => CurrentUsage >= LimitValue;

        /// <summary>
        /// 是否需要重置
        /// </summary>
        [NotMapped]
        public bool NeedsReset => ResetPeriodMinutes.HasValue && LastResetAt.HasValue && 
                                 DateTime.UtcNow.Subtract(LastResetAt.Value).TotalMinutes >= ResetPeriodMinutes.Value;
    }

    /// <summary>
    /// 租户资源使用历史记录
    /// 用于记录租户资源使用的历史数据，便于分析和监控
    /// </summary>
    [Table("tenant_resource_usage_history")]
    public class TenantResourceUsageHistory : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 资源配额ID
        /// </summary>
        public long ResourceQuotaId { get; set; }

        /// <summary>
        /// 资源类型
        /// </summary>
        [Required]
        [StringLength(100)]
        public string ResourceType { get; set; } = string.Empty;

        /// <summary>
        /// 使用量变化
        /// 正数表示增加，负数表示减少
        /// </summary>
        public long UsageChange { get; set; }

        /// <summary>
        /// 变化后的总使用量
        /// </summary>
        public long TotalUsageAfter { get; set; }

        /// <summary>
        /// 操作类型
        /// 如：increment, decrement, reset等
        /// </summary>
        [StringLength(50)]
        public string OperationType { get; set; } = string.Empty;

        /// <summary>
        /// 操作来源
        /// 如：api, system, admin等
        /// </summary>
        [StringLength(100)]
        public string? OperationSource { get; set; }

        /// <summary>
        /// 记录时间
        /// </summary>
        public DateTime RecordedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 操作描述
        /// </summary>
        [StringLength(500)]
        public string? Description { get; set; }

        /// <summary>
        /// 扩展数据
        /// JSON格式存储额外信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? Metadata { get; set; }

        // 导航属性
        public virtual TenantResourceQuota ResourceQuota { get; set; } = null!;
    }
}
